#!/bin/bash



BUNDLERESOURCEPATH="$(dirname "$0")/../Resources"

echo "==============================="
echo "=== L U M I K I T   S H O W ==="
echo "==============================="

#find wine, try in Bundle, ~/Applications, /Applications, Spotlight
if [ -f "$BUNDLERESOURCEPATH/Wine.bundle/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$BUNDLERESOURCEPATH/Wine.bundle/Contents/Resources"
elif [ -f "$HOME/Applications/Wine.app/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$HOME/Applications/Wine.app/Contents/Resources"
elif [ -f "/Applications/Wine.app/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="/Applications/Wine.app/Contents/Resources"
elif [ -f "$(mdfind 'kMDItemCFBundleIdentifier == org.kronenberg.Wine' | grep -m 1 'Wine.app')/Contents/Resources/bin/wine" ]; then
    export WINEUSRPATH="$(mdfind 'kMDItemCFBundleIdentifier == org.kronenberg.Wine' | grep -m 1 'Wine.app')/Contents/Resources"
else
    echo "Wine not found!"
    exit 1
fi


# create working copy
export WINEPREFIX="$HOME/Library/Application Support/com.lumikit.show_150756303810856"
if [ ! -d "$WINEPREFIX" ]; then
    "$BUNDLERESOURCEPATH/Winetricks.app/Contents/MacOS/./Winetricks" "$BUNDLERESOURCEPATH/wineprefix" "$WINEPREFIX" "$(defaults read "$BUNDLERESOURCEPATH/../Info" CFBundleName)"
fi



# exports (we keep X11 for fallback)
export PATH="$WINEUSRPATH/bin":$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$WINEUSRPATH/lib":"/opt/X11/lib":"/usr/X11/lib"
export DYLD_FALLBACK_LIBRARY_PATH="/usr/lib:$WINEUSRPATH/lib":"/opt/X11/lib":"/usr/X11/lib"
export WINEPATH="$WINEUSRPATH/bin"

#some default windows vars that might be missing ( http://ss64.com/nt/syntax-variables.html )
[ -z "$COMPUTERNAME" ] && export COMPUTERNAME="$(/usr/sbin/scutil --get ComputerName)"

#if the exe file size differs, copy all "drive_c" files again
export FN_1="$HOME/Library/Application Support/com.lumikit.show_150756303810856/drive_c/Program Files/Lumikit/LumikitSHOW.exe"
export FN_2="$BUNDLERESOURCEPATH/wineprefix/drive_c/Program Files/Lumikit/LumikitSHOW.exe"

export FS_1=$(wc -c < "$FN_1")
export FS_2=$(wc -c < "$FN_2")

echo "MAC EXE SIZE = $FS_1"
echo "APP EXE SIZE = $FS_2"

if [ "$FS_1" != "$FS_2" ]; then
    echo "================================="
    echo "=== S T A R T  N E W  C O P Y ==="
    echo "================================="
    echo "FROM - $BUNDLERESOURCEPATH/wineprefix/drive_c/"
    echo "TO - $HOME/Library/Application Support/com.lumikit.show_150756303810856/drive_c/."
    echo "================================="
    cp -a "$BUNDLERESOURCEPATH/wineprefix/drive_c/." "$HOME/Library/Application Support/com.lumikit.show_150756303810856/drive_c/"
    echo "================================="
fi


# start wine and program, if possible in the programs root directory, change to forwardslashes
BIN_FILE="$(sed 's|\\|/|g' <<< "$(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramPath)")"
BIN_PATH="$BUNDLERESOURCEPATH/wineprefix/drive_c$(sed 's|C:||' <<< "$(dirname "$BIN_FILE")")"
if [ -d "$BIN_PATH" ]; then
    cd "$BIN_PATH"
fi

if [ "$(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramArguments)" != "" ]; then
"$WINEUSRPATH/bin/wine" "$BIN_FILE" $(defaults read "$BUNDLERESOURCEPATH/../Info" WineProgramArguments)
else
"$WINEUSRPATH/bin/wine" "$BIN_FILE"
fi
